package com.ygqh.baby.service.impl;
import java.util.List;

import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminMaterial;
import  com.ygqh.baby.po.YgMaterial;
import  com.ygqh.baby.mapper.YgMaterialMapper;
import  com.ygqh.baby.service.YgMaterialService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.YgMaterialExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class YgMaterialServiceImpl implements YgMaterialService{
	
	@Autowired
	private YgMaterialMapper ygMaterialMapper;
	
	@Override
	public List<YgMaterial> find() {
		YgMaterialExample example = new YgMaterialExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygMaterialMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgMaterialExample example = new YgMaterialExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygMaterialMapper.countByExample(example);
	}

	@Override
	public YgMaterial findById(Long id) {
		return ygMaterialMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(YgMaterial ygMaterial) {
		return ygMaterialMapper.insertSelective(ygMaterial);
	}

	@Override
	public int update(YgMaterial ygMaterial) {
		return ygMaterialMapper.updateByPrimaryKeySelective(ygMaterial);
	}

	@Override
	public ResultSet<YgMaterial> search(QueryInfo queryInfo,String q) {
		List<YgMaterial>  list =  ygMaterialMapper.selectSuper(queryInfo, q);
		int count = ygMaterialMapper.countSuper(q);
		ResultSet<YgMaterial> resultSet = new ResultSet<YgMaterial>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgMaterialExample example = new YgMaterialExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgMaterial record = new YgMaterial();
		record.setStatus(DataStatus.Delete);

		return ygMaterialMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgMaterial ygMaterial) {
		int r = 0;
		if(ygMaterial.getId() != null) {
			r = this.update(ygMaterial);
		}else {
			r = this.save(ygMaterial);
		}
		return r;
	}

	@Override
	public int findName(String name, Long id) {		
		return ygMaterialMapper.selectByName(name, id);
	}
	@Override
	public List<YgMaterial> findByNameList(List<String> materialList) {
		return ygMaterialMapper.selectByNameList(materialList);
	}


}

